package com.aco.party.common.util;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <P>Title: CommondUtils</P>
 * <P>Description: create by XSDToJava V1.0</P>
 * <P>Copyright: Copyright (c) 2013 </P>
 * <P>Company: xunzuo </P>
 * 
 * @author lj
 * @version 1.0
 * @create 2013-9-24
 * @history
 */
public class CommandUtils {
	private static final Logger logger = LoggerFactory.getLogger(CommandUtils.class);
	/** 换行符 **/
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");

	/**
	 * 执行一条命令
	 * 
	 * @param execStr
	 *            String 命令字符串
	 * @return String 执行命令错误时的信息。
	 */
	public static String executeCommand(String execStr) {
		Runtime runtime = Runtime.getRuntime(); // 取得当前运行期对象
		String outInfo = ""; // 执行错误的输出信息
		try {
			String[] args = null;
			if (isWindowsOS()) {
				// 执行windows下的命令
				args = new String[] { "cmd", "-c", execStr };
			} else if (isLinuxOS()) {
				// 执行linux下的命令
				args = new String[] { "sh", "-c", execStr };
			} else {
				return "不支持" + getSystemName() + "系统！";
			}
			Process proc = runtime.exec(args); // 启动另一个进程来执行命令
			outInfo = IOUtils.toString(proc.getErrorStream(), "GBK");
			// 检查命令是否失败。
			try {
				if (proc.waitFor() != 0) {
					logger.error("exit failer value=" + proc.exitValue() + LINE_SEPARATOR + "errMsg=" + LINE_SEPARATOR + outInfo);
				}
			} catch (Exception e) {
				logger.error("executeCommand(String) - exception ignored" + e.getMessage(), e); //$NON-NLS-1$
			}
		} catch (Exception e) {
			logger.error("executeCommand(String) - exception ignored" + e.getMessage(), e); //$NON-NLS-1$
		}
		if (logger.isInfoEnabled()) {
			logger.info("executeCommand(String) - end"); //$NON-NLS-1$
		}
		return outInfo;
	}

	/**
	 * getSystemName
	 * 
	 * @return
	 */
	public static String getSystemName() {
		if (logger.isDebugEnabled()) {
			logger.debug("getSystemName() - start"); //$NON-NLS-1$
		}

		String returnString = System.getProperty("os.name");

		if (logger.isDebugEnabled()) {
			logger.debug("getSystemName() - end, String returnString=" + returnString); //$NON-NLS-1$
		}
		return returnString;
	}

	/**
	 * isWindowsOS
	 * 
	 * @return
	 */
	public static boolean isWindowsOS() {
		if (logger.isDebugEnabled()) {
			logger.debug("isWindowsOS() - start"); //$NON-NLS-1$
		}

		boolean returnboolean = getSystemName().toLowerCase().indexOf("window") != -1;
		if (logger.isDebugEnabled()) {
			logger.debug("isWindowsOS() - end"); //$NON-NLS-1$
		}
		return returnboolean;
	}

	/**
	 * isLinuxOS
	 * 
	 * @return
	 */
	public static boolean isLinuxOS() {
		if (logger.isDebugEnabled()) {
			logger.debug("isLinuxOS() - start"); //$NON-NLS-1$
		}

		boolean returnboolean = getSystemName().toLowerCase().indexOf("linux") != -1;
		if (logger.isDebugEnabled()) {
			logger.debug("isLinuxOS() - end"); //$NON-NLS-1$
		}
		return returnboolean;
	}

	/**
	 * 以GBK为标准，返回服务器系统的编码变量的boolean型
	 * 
	 * @return boolean true ---是GBK编码 false---非GBK编码
	 */
	public static boolean isGBKsystem() {
		return System.getProperty("file.encoding").indexOf("GB") >= 0;
	}

	public static void main(String[] args) throws IOException {
		StringBuffer sb = new StringBuffer();
		sb.append("ftp").append(" 192.168.8.234").append("portalbg 000111");
		executeCommand(sb.toString());
		// Runtime.getRuntime().exec("ftp");
		// Runtime.getRuntime().exec("open 192.168.8.234");
		// Runtime.getRuntime().exec("portalbg 000111");
		// 处理文件格式
		executeCommand("dos2unix " + "*.txt");
	}
}
